package hyit.app.servlet.wx;

import hyit.app.factory.DAOFactory;
import hyit.app.model.TeacherInfo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class ValidateWX
 */
@WebServlet("/validateWX")
public class ValidateWX extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ValidateWX() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		JSONObject jsonObj = new JSONObject();
		TeacherInfo teacherInfo = new TeacherInfo();
		teacherInfo.setAccount(account);
		teacherInfo.setPassword(password);
		try {
			teacherInfo = DAOFactory.getITeacherInfoDAOInstance().check(
					teacherInfo);
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(teacherInfo!=null){
			jsonObj.put("teacherNumber", teacherInfo.getTeacherNumber());
			jsonObj.put("name", teacherInfo.getName());
			jsonObj.put("rank", teacherInfo.getRank());
			jsonObj.put("departmentNumber", teacherInfo.getDepartmentNumber());
			jsonObj.put("msg", "true");
		}else{
			jsonObj.put("msg", "false");
		}
		PrintWriter pw = response.getWriter();
		pw.print(jsonObj);
		pw.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
